package com.example.singleactivitytemplate.ui.acts

import android.view.View
import android.widget.Toast
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.fragment.NavHostFragment
import com.example.singleactivitytemplate.R
import com.example.singleactivitytemplate.databinding.ActivityMainBinding
import com.example.singleactivitytemplate.ui.base.BaseActivity


class MainActivity : BaseActivity<ActivityMainBinding, MainActViewModel>(), View.OnClickListener,
    View.OnLongClickListener {
    private var navHostController: NavController? = null

    override fun getViewBindingClass(): ActivityMainBinding =
        ActivityMainBinding.inflate(layoutInflater)

    override fun getViewModelClass() = MainActViewModel::class.java
    override fun initData() {
        //设置主界面
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_main_fragment) as NavHostFragment?
        navHostController = navHostFragment?.navController
    }

    private fun switchFragment(fragmentId: Int) {
        if (navHostController?.popBackStack(fragmentId, false) == false) {
            val builder = NavOptions.Builder().apply {
                setLaunchSingleTop(true)
//                setEnterAnim(R.anim.fade_in_right)
//                setExitAnim(R.anim.fade_out_left)
//                setPopEnterAnim(R.anim.fade_in_left)
//                setPopExitAnim(R.anim.fade_out_right)
            }
            navHostController?.navigate(fragmentId, null, builder.build())
        }
    }

    override fun initObserver() {
        // 实现细节
        mViewModel?.mFragmentId?.observe(this) {



//            if (it == R.id.mainFragment) {
//                viewBinding?.title?.root?.visibility = View.INVISIBLE
//                viewBinding?.logoIv?.visibility = View.VISIBLE
//            } else {
//                viewBinding?.title?.root?.visibility = View.VISIBLE
//                viewBinding?.logoIv?.visibility = View.INVISIBLE
//            }
//            if(it==R.id.SettingFragment)
//            {
//                viewBinding?.title?.kylinLogo?.setOnLongClickListener(this)
//            }else{
//                viewBinding?.title?.kylinLogo?.setOnLongClickListener(null)
//            }


            switchFragment(it)
        }

    }

    override fun destroy() {

    }


    override fun initOnListener() {
        viewBinding?.title?.btnSwitchSystem?.setOnClickListener(this)
        viewBinding?.title?.btnSetting?.setOnClickListener(this)
        viewBinding?.title?.btnAlarmSound?.setOnClickListener(this)
        viewBinding?.title?.titleTime?.setOnClickListener(this)

        viewBinding?.title?.kylinLogo?.setOnLongClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v) {
            //返回按键
            viewBinding?.title?.btnSwitchSystem -> {
                Toast.makeText(this,"返回", Toast.LENGTH_SHORT).show()
            }
            //设置按键
            viewBinding?.title?.btnSetting -> {
                mViewModel?.mFragmentId?.value = R.id.SettingFragment
            }

        }
    }

    override fun onLongClick(v: View?): Boolean {


        return true

    }


}
